package view;

import java.awt.BorderLayout; 
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import battleModel.BattleModelOld;
import battleModel.TankOld;

@SuppressWarnings("serial")
public class BattleViewOld extends MasterViewPanel {

	private BattleModelOld bm;
	private TankOld player;

	private GameButton back;
	private BufferedImage tileImage;
	private Image tank, turret;

	private Rectangle battleWindow;
	private GraphicsEnvironment ge;
	private GraphicsDevice gd;

	private AffineTransform tankAffine, turretAffine, mapAffine;
	private double tankAngle, turretAngle;

	public BattleViewOld(MasterView m, BattleModelOld bm) {
		super(m);

		this.bm = bm;
		this.player = bm.getPlayer();

		ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
		gd = ge.getDefaultScreenDevice();
		battleWindow = ge.getMaximumWindowBounds();

		setWindowSize();
		setUpLayout();
		scaleObjects();
		setTank(player.getLocation());

		TankKeyLis key = new TankKeyLis();
		this.setFocusable(true);
		this.addKeyListener(key);

		MouseLis mouse = new MouseLis();
		this.setFocusable(true);
		this.addMouseListener(mouse);
		MouseMotionLis mouseMotion = new MouseMotionLis();
		this.addMouseMotionListener(mouseMotion);

	}

	private void setWindowSize() {
		m.setResizable(true);
		m.setSize(battleWindow.width, battleWindow.height);
		m.setLocation(0, 0);
		m.setResizable(false);
	}

	private void scaleObjects() {

		mapAffine = new AffineTransform();
		mapAffine.scale((double) battleWindow.width / 1300,
				(double) battleWindow.height / 920);

		tankAffine = new AffineTransform();
		tankAffine.scale((double) battleWindow.width / 1300,
				(double) battleWindow.height / 920);

		turretAffine = new AffineTransform();
		turretAffine.scale((double) battleWindow.width / 1300,
				(double) battleWindow.height / 920);
	}

	private void setTank(Point startLocation) {
		int x = startLocation.x, y = startLocation.y;
		tankAffine.translate(x, y);
		turretAffine.translate(x, y);

		tankAngle = player.getTankAngle();
		turretAngle = player.getTurretAngle();
	}

	private void setUpLayout() {
		this.setLayout(new BorderLayout());
		try {
			tileImage = ImageIO.read(new File("images/maps/sandMap.png"));
			tank = ImageIO.read(new File("images/tanks/TankSprite.png"))
					.getSubimage(0, 125, 63, 63);
			turret = ImageIO.read(new File("images/tanks/TankSprite.png"))
					.getSubimage(0, 375, 63, 63);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void pause() {
		PopUpWindow pauseWindow = new PopUpWindow(m, PopUps.PAUSE);
	}

	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2D = (Graphics2D) g;

		g2D.drawImage(tileImage, mapAffine, null);
		g2D.drawImage(tank, tankAffine, null);
		g2D.drawImage(turret, turretAffine, null);
	}

	private class TankKeyLis implements KeyListener {

		@Override
		public void keyPressed(KeyEvent e) {

			double speed = player.speed();

			// tank head oriented
			if (e.getKeyCode() == KeyEvent.VK_UP) {
				tankAffine.translate(speed, 0);
				turretAffine.translate(
						speed * Math.cos(tankAngle - turretAngle),
						speed * Math.sin(tankAngle - turretAngle));
			} else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
				tankAffine.translate(-speed / 2, 0);
				turretAffine.translate(
						(-speed / 2) * Math.cos(tankAngle - turretAngle),
						(-speed / 2) * Math.sin(tankAngle - turretAngle));
			} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
				tankAffine.rotate(Math.PI / 32, 31.25, 31.25);
				tankAngle += Math.PI / 32;
			} else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
				tankAffine.rotate(-Math.PI / 32, 31.25, 31.25);
				tankAngle -= Math.PI / 32;
			} else if (e.getKeyCode() == KeyEvent.VK_P) {
				pause();
			}
			repaint();

			/*
			 * /turret head oriented else if (e.getKeyCode() ==
			 * KeyEvent.VK_RIGHT) { turretAffine.rotate(Math.PI/32, 31, 30.5);
			 * turretAngle+=Math.PI/32; repaint(); } else if (e.getKeyCode() ==
			 * KeyEvent.VK_LEFT) { turretAffine.rotate(-Math.PI/32, 31, 30.5);
			 * turretAngle-=Math.PI/32; repaint(); }
			 */

		}

		@Override
		public void keyReleased(KeyEvent e) {
		}

		@Override
		public void keyTyped(KeyEvent e) {
		}

	}

	private class MouseLis implements MouseListener {

		@Override
		public void mouseClicked(MouseEvent arg0) {
			// System.out.println(arg0.getX());
			// System.out.println(arg0.getY());
		}

		@Override
		public void mouseEntered(MouseEvent arg0) {
			// System.out.println(arg0.getX());
			// System.out.println(arg0.getY());
		}

		@Override
		public void mouseExited(MouseEvent arg0) {
		}

		@Override
		public void mousePressed(MouseEvent arg0) {
		}

		@Override
		public void mouseReleased(MouseEvent arg0) {
		}

	}

	private class MouseMotionLis implements MouseMotionListener {

		@Override
		public void mouseDragged(MouseEvent arg0) {
		}

		@Override
		public void mouseMoved(MouseEvent e) {
			double x, y, tempAngle = turretAngle;
			y = e.getY() - 30.5 - turretAffine.getTranslateY();
			x = e.getX() + 31 - turretAffine.getTranslateX();
			turretAngle = Math.atan(y / x);
			if (x < 0)
				turretAngle -= Math.PI;
			
			turretAffine.rotate(turretAngle - tempAngle, 31, 30.5);
			repaint();
		}

	}
	
	/**
	 * String representation of panel name.
	 */
	public String toString() {
		return "BATTLE";
	}

}
